/**
 *author:yhb
 */
package com.towker.frame.controller.rbac;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.rbac.Role;
import com.towker.frame.service.rbac.role.RoleService;
import com.towker.web.MyRequest;

@Controller
@RequestMapping("/manage/rbac/role")
public class RoleController
{
	private RoleService service;
	public void setRoleService(RoleService service){this.service = service;}
	/**
	 * 添加角色*/
	@RequestMapping("/addRole{viewName}")
	public ModelAndView addRole(HttpServletRequest request, HttpServletResponse response, Role role, @PathVariable
	String viewName) throws Exception
	{
		ModelAndView result = null;
		
		if ("1".equals(viewName)){
			MyRequest req = new MyRequest(request);
			long systemid = req.getLong("systemid");
			result = new ModelAndView();
			result.addObject("systemid", systemid);
			result.setViewName("/manage/rbac/role/addRole.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.add(role);
				writer.write("添加系统角色成功！");
			}catch (Exception e){e.printStackTrace();writer.write("添加系统角色失败！"+e.getCause());}
		}
		return result;
	}
	/**
	 * 根据编号删除角色*/
	@RequestMapping("/delRole")
	public void delRoleById(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect( req.getRefererURL());
	}
	/**
	 * 修改角色*/
	@RequestMapping("/updRole{viewName}")
	public ModelAndView updRole(HttpServletRequest request, HttpServletResponse response, Role newRole, @PathVariable
	String viewName) throws Exception
	{
		ModelAndView result=null;
		if ("1".equals(viewName)){
			MyRequest req = new MyRequest(request);
			long id = req.getLong("keyIndex");
			Role role = service.getById(id);
			result = new ModelAndView();
			result.addObject("role", role);
			result.setViewName("/manage/rbac/role/updRole.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.update(newRole);
				writer.write("修改系统角色成功！");
			}catch (Exception e){writer.write("修改系统角色失败！");e.printStackTrace();}
		}
		return result;
	}
	/**
	 * 根据系统获得角色列表*/
	@SuppressWarnings({"unchecked", "unchecked"})
	@RequestMapping("/getRole")
	public ModelAndView getRole(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1")
	int page) throws IOException
	{
		int pageSize=10;
		MyRequest req=new MyRequest(request);
		Map map=req.getParameterValueMap(false, true);
		PageModel<Role> pageModel=service.getPage(page,pageSize, map);
		PageNav pageNav=new PageNav(request,pageModel);
		ModelAndView result=new ModelAndView();
		result.addObject("pageModel",pageModel);
		result.addObject("pageNav",pageNav);
		result.setViewName("/manage/rbac/role/getRole.jsp");
		return result;
	}
	/**
	 * 查看系统角色详细信息*/
	@RequestMapping("/getRoleById")
	public ModelAndView getRoleById(HttpServletRequest request)
	{
		MyRequest req = new MyRequest(request);
		long id = req.getLong("keyIndex");
		Role role = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("role", role);
		result.setViewName("/manage/rbac/role/getRoleById.jsp");
		return result;
	}
	/**
	 * 获得系统角色树*/
	@RequestMapping("/getSysRoleTree")
	public void getSysRoleTree(HttpServletResponse response) throws IOException
	{
		PrintWriter writer = response.getWriter();
		String str = service.getSysRoleTree();
		writer.write(str);
	}
	/**
	 * 获得用户分配的系统角色树*/
	@RequestMapping("/getSysRoleTreeOfUser")
	public void getSysRoleTreeOfUser(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		PrintWriter writer = response.getWriter();
		MyRequest req=new MyRequest(request);
		String str = service.getSysRoleTreeOfUser(req.getLong("userid"));
		writer.write(str);
	}
}
